package java6681.sort.set;
import java.util.Comparator;
import java.util.TreeSet;
/**
 * @author Junichiro
 * @description TreeSet的排序
 * @date 2021-04-08 12:25
 **/

public class SetSort6681 {
    public static void main(String[] args) {
        /*按成绩降序*/
        TreeSet<Course6681> list1 = new TreeSet<>();
        Course6681[] c = addSet();
        for (Course6681 course6681 : c) {
            list1.add(course6681);
        }
        System.out.println("-------------按成绩降序-------------");
        for (Course6681 cList1 : list1) {
            System.out.println(cList1);
        }

        /*按学期升序*/
        TreeSet<Course6681> list2 = new TreeSet<>(new Comparator<Course6681>() {
            @Override
            public int compare(Course6681 c1, Course6681 c2) {
                String cSb1 = c1.getTerm().substring(0, 1);
                String cSb2 = c2.getTerm().substring(0, 1);
                int num = Integer.parseInt(cSb1) - Integer.parseInt(cSb2);
                int num2 = num == 0 ? c1.getTerm().substring(1, 2).compareTo(c2.getTerm().substring(1, 2)) : num;
                return num2;
            }
        });
        for (Course6681 course6681 : c) {
            list2.add(course6681);
        }
        System.out.println("-------------按学期升序-------------");
        for (Course6681 cList2 : list2) {
            System.out.println(cList2);
        }

        /*按学分升序*/
        TreeSet<Course6681> list3 = new TreeSet<>(new SorterByCredit6681());
        for (Course6681 course6681 : c) {
            list3.add(course6681);
        }
        System.out.println("-------------按学分升序-------------");
        for (Course6681 course6681 : list3) {
            System.out.println(course6681);
        }

        /*按学期、学分多关键字排序（升序）*/
        TreeSet<Course6681> list4 = listAdd();
        System.out.println("----------按学期、学分多关键字排序（升序）----------");
        for (Course6681 cList4 : list4) {
            System.out.println(cList4);
        }

    }

    /*按学期、学分多关键字排序（升序）*/
    public static TreeSet<Course6681> listAdd() {
        Course6681[] c = addSet();
        TreeSet<Course6681> list = new TreeSet<>(new Comparator<Course6681>() {
            @Override
            public int compare(Course6681 c1, Course6681 c2) {
                String cSb1 = c1.getTerm().substring(0, 1);
                String cSb2 = c2.getTerm().substring(0, 1);
                int num = Integer.parseInt(cSb1) - Integer.parseInt(cSb2);
                int num2 = num == 0 ? c1.getTerm().substring(1, 2).compareTo(c2.getTerm().substring(1, 2)) : num;
                int num3 = num2 == 0 ? c1.getCredit() - c2.getCredit() : num2;
                return num3;
            }
        });
        for (Course6681 course6681 : c) {
            list.add(course6681);
        }
        return list;
    }

    public static Course6681[] addSet() {
        Course6681[] course6681s = new Course6681[]
                {
                        new Course6681("Java", 3, 89, "1下", "A001"),
                        new Course6681("Python", 2, 92, "2上", "A002"),
                        new Course6681("C++", 2, 78, "2下", "A003"),
                        new Course6681("HTML+CSS", 2, 69, "3上", "A004"),
                        new Course6681("计算机网络", 4, 79, "1上", "A005")

                };
        return course6681s;
    }
}
